package com.anpy.bill.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.anpy.bill.entity.BankEntity;
import com.anpy.bill.entity.CardEntity;
import com.anpy.bill.entity.PayTypeEntity;
import com.anpy.bill.mapper.BankMapper;
import com.anpy.bill.mapper.CardMapper;
import com.anpy.bill.mapper.PayTypeMapper;
import com.anpy.bill.model.param.DepositParam;
import com.anpy.bill.model.param.SaveBankParam;
import com.anpy.bill.service.CardService;
import com.anpy.common.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author : anpy
 * @create 2024/1/22 17:08
 */
@Service
public class CardServiceImpl extends BaseServiceImpl<CardMapper, CardEntity> implements CardService {

    @Resource
    private PayTypeMapper payTypeMapper;
    @Resource
    private BankMapper bankMapper;
    @Override
    public boolean saveDeposit(DepositParam depositParam) {
        List<CardEntity> list = lambdaQuery().eq(CardEntity::getCardNumber, depositParam.getCardNumber()).list();
        if (CollUtil.isNotEmpty(list)) {
            return false;
        }

        CardEntity cardEntity = new CardEntity();
        BeanUtils.copyProperties(depositParam, cardEntity);
        save(cardEntity);

        return true;
    }

    @Override
    public List<CardEntity> cardList(Integer depositType, Long userId) {
        return lambdaQuery()
                .eq(CardEntity::getUserId, userId)
                .eq(CardEntity::getDepositType, depositType)
                .list();
    }

    @Override
    public List<PayTypeEntity> payTypeList(Long userId) {
        LambdaQueryWrapper<PayTypeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PayTypeEntity::getUserId, userId);
        return payTypeMapper.selectList(wrapper);
    }

    @Override
    public List<BankEntity> bankList(Long userId, Integer type) {
        LambdaQueryWrapper<BankEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BankEntity::getUserId, userId);
        if(type !=null){
            wrapper.eq(BankEntity::getType, type);
        }

        return bankMapper.selectList(wrapper);
    }

    @Override
    public int saveBank(SaveBankParam saveBankParam) {
        BankEntity bankEntity = new BankEntity();
        BeanUtils.copyProperties(saveBankParam, bankEntity);
        return bankMapper.insert(bankEntity);
    }


}
